package com.ncloud.resman.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.entity.RepairRecord;
import com.ncloud.resman.service.impl.RepairRecordImpl;


/**
 * user:sundelong  date:2017年8月11日
 */
@RestController
@RequestMapping("/resman/repair")
public class RepairRecordController {
	@Autowired
	public RepairRecordImpl repairRecordService;
	
	
	@PostMapping("/list")
	public Result<?> getRepairList(HttpServletRequest request,@RequestBody JSONObject json){
		Object infos=repairRecordService.getRepairList(json);
		return ResultUtil.success(infos);
	}
	
	@PostMapping(value = "/add")
    public Result<?> addRepair(HttpServletRequest request, @RequestBody JSONObject json){
		RepairRecord repairRecord=new RepairRecord();
		repairRecord.setCreateUser(Long.parseLong(request.getAttribute("userID").toString()));
		repairRecord.setEqptType(json.getString("eqptType"));
		repairRecord.setSnId(json.getString("snId"));
		repairRecord.setRepairState(json.getString("repairState"));
		repairRecord.setRepairDate(json.getDate("repairDate"));
		repairRecord.setSolution(json.getString("solution"));
		
		int n=repairRecordService.insertRepair(repairRecord);
		return ResultUtil.success(n);
	}
	
	@PostMapping(value="/update")
	public Result<?> updateRepair(HttpServletRequest request,@RequestBody JSONObject json){
		json.put("updateUser", request.getAttribute("userID"));
		int n=repairRecordService.updateRepair(json);
		return ResultUtil.success(n);		
	}
	
	@PostMapping(value = "/delete")
	public Result<?> deleteRepair(HttpServletRequest request,@RequestBody JSONObject json){
		int n=repairRecordService.deleteRepair(json);
		return ResultUtil.success(n);		
	}
	
	@PostMapping(value = "/select")
	public Result<?> getRepairInfoBySnId(HttpServletRequest request,@RequestBody JSONObject json){	
		Object array=repairRecordService.getRepairInfoBySnId(json);
		return ResultUtil.success(array);		
	}
	//导出Excel
	@GetMapping(value ="/export")
    public Result<?> exportBrand(HttpServletRequest request,HttpServletResponse response) throws IOException{ 
		String exportType=request.getParameter("exportType");
    	JSONObject jsonObject=new JSONObject();
    	jsonObject.put("exportType",request.getParameter("exportType"));
    	jsonObject.put("id",request.getParameter("id"));
    	jsonObject.put("eqptType",request.getParameter("eqptType"));
    	jsonObject.put("snId",request.getParameter("snId"));
    	jsonObject.put("repairState",request.getParameter("repairState"));
    	jsonObject.put("beginDate",request.getParameter("beginDate"));
    	jsonObject.put("endDate",request.getParameter("endDate"));
		repairRecordService.exportRepair(jsonObject, response);
		return ResultUtil.success();
    }
	
}
